package com.yycx.module.bus.provider.handler.supplierbond;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusOrderService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 垫付款保存扩展器
 *
 * @author zyf
 */
@Component("supplierBondPaymentSaveHandler")
public class SupplierBondPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Autowired
    private BusOrderService orderService;

    @Autowired
    private BusOrderDetailsService detailsService;

    @Autowired
    private BusSupplierService supplierService;





    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String orderId = payment.getOrderId();
        ApiAssert.isNotEmpty("订单ID不能为空", orderId);
        BusOrder order = orderService.getById(orderId);
        Long supplierId = order.getSupplierId();
        BusSupplier supplier = supplierService.getById(supplierId);
        String orderNo = order.getOrderNo();
        payment.setPaymentNo("DF" + orderNo);
        payment.setTitle("支付" + supplier.getSupplierName() + "保证金");
        //未提交
        payment.setPaymentState(CommonConstants.INT_0);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setOrderId(orderId);
        payment.setCompanyId(order.getCompanyId());
        payment.setSupplierId(order.getSupplierId());
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setCustomId(order.getCustomId().toString());
        payment.setOrganizationId(order.getOrganizationId());
        payment.setDeptId(order.getDeptId());
        payment.setAmountType(AmountTypeEnum.SupplierBond.getCode());
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String orderId=entity.getOrderId();
        //更新垫付款状态
        detailsService.updateSupplierBondState(null,Long.parseLong(orderId),CommonConstants.INT_1);
    }


}
